<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <div class="shop-card">
        <div
          class="card-content"
          v-for="(item, i) in cart"
          :key="item.goods_id"
        >
          <div class="card-content-inner flex">
            <!-- 复选框 -->
            <div class="cart-checkbox">
              <label>&nbsp;</label>
              <input
                type="checkbox"
                :checked="item.selected"
                @click="handleCheckbox(i)"
              />
            </div>

            <!-- 中间商品图片 -->
            <img :src="item.goods_small_logo" />
            <!-- 右侧部分 -->
            <div class="info">
              <h1 class="van-multi-ellipsis--l2">{{ item.goods_name }}</h1>
              <div class="flex goodsCard-bottom">
                <span class="price">¥{{ item.goods_price }}</span>
                <div class="step">
                  <button @click="down(i)" class="step-down">-</button>
                  <input type="text" :value="item.num" class="step-input" />
                  <button @click="add(i)" class="step-add">+</button>
                </div>
                <a href="#" @click.prevent="remove(i)">删除</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <van-submit-bar
      :disabled="this.isDisabled"
      class="submit"
      :price="TotalPrice * 100"
      button-text="提交订单"
      @submit="onSubmit()"
    >
      <template>
        已勾选商品&nbsp;<span class="goodsnum">{{ getSelectedCount }}</span
        >&nbsp;件
      </template>
    </van-submit-bar>
  </div>
</template>
<script>
import { mapState, mapGetters } from "vuex";
export default {
  data() {
    return {
      isDisabled: false,
      count: 1,
      i: 0,
      num: 0,
      shopcart_orderlist: {
        order_id: null,
        order_price: this.TotalPrice,
        order_create_time: this.formatDate(),
        order_address: {},
        goods_list: [],
      },
    };
  },
  computed: {
    ...mapState("shopcart", ["cart"]),
    ...mapGetters("shopcart", ["getSelectedCount", "TotalPrice"]),
    ...mapGetters("order", ["getLength"]),
    ...mapGetters("address", ["getAddress"]),
  },
  created() {
    if (this.getSelectedCount == 0) {
      this.isDisabled = true;
    } else {
      this.isDisabled = false;
    }
  },
  methods: {
    add(i) {
      this.$store.commit("shopcart/addcount", i);
    },
    down(i) {
      this.$store.commit("shopcart/downcount", i);
      if (this.getSelectedCount == 0) {
        this.isDisabled = true;
      } else {
        this.isDisabled = false;
      }
    },
    remove(index) {
      this.cart.splice(index, 1);
    },
    onSubmit() {
      let temp = [];
      this.cart.forEach((element) => {
        if (element.selected) {
          temp.push(element);
        }
      });
      this.shopcart_orderlist.order_address = this.getAddress[0];
      console.log(this.getAddress[0]);
      this.shopcart_orderlist.goods_list = temp;
      this.shopcart_orderlist.order_create_time = this.formatDate();
      this.shopcart_orderlist.order_id = this.getLength + 1;
      this.shopcart_orderlist.order_price = this.TotalPrice;
      this.$router.push({
        name: "order_create",
        params: {
          shopcart_orderlist: this.shopcart_orderlist,
        },
      });
    },
    handleCheckbox(i) {
      this.cart[i].selected = !this.cart[i].selected;
    },
    formatDate() {
      var date = new Date();
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },
  },
};
</script>
<style lang="scss" scoped>
.step-input {
  box-sizing: border-box;
  width: 32px;
  height: 28px;
  margin: 0 2px;
  padding: 0;
  color: #323233;
  font-size: 14px;
  line-height: normal;
  text-align: center;
  vertical-align: middle;
  background-color: #f2f3f5;
  border: 0;
  border-width: 1px 0;
  border-radius: 0;
}
.step-down,
.step-add {
  position: relative;
  box-sizing: border-box;
  width: 28px;
  height: 28px;
  margin: 0;
  padding: 0;
  color: #323233;
  vertical-align: middle;
  background-color: #f2f3f5;
  border: 0;
  cursor: pointer;
}
.goodsnum {
  color: red;
  font-size: 24px;
}
.submit {
  position: fixed;
  bottom: 50px;
}
.flex {
  display: flex;
}
.shopcar-container {
  overflow: hidden;

  .goods-list {
    padding-bottom: 50px;
    .shop-card {
      .card-content {
        .card-content-inner {
          align-items: center;
          padding: 10px;

          .cart-checkbox {
            padding-left: 10px;
            padding-right: 10px;
            padding-bottom: 10px;
          }
          img {
            width: 60px;
            height: 60px;
          }

          .info {
            margin-left: 10px;
            width: 100%;
            overflow: hidden;
            box-sizing: border-box;
            h1 {
              font-size: 13px;
              font-weight: bold;
              line-height: 20px;
              padding-top: 10px;
            }

            .goodsCard-bottom {
              display: flex;
              flex-direction: row;
              align-items: center;
              justify-content: space-between;
              .price {
                text-align: left;
                font-size: 16px;
                font-weight: 700;
                color: red;
                flex: 1;
              }
              .step {
                margin: 0 20px 0 0;
              }
              a {
                line-height: 25px;
                color: #39a9ed;
              }
            }
          }
        }
      }
    }
  }
}
</style>
